import axios from 'axios'
// axios.defaults.baseURL = 'http://192.168.1.110:80';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

class API {
    static get = function (url, params) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'get',
                url,
                params: params,
            }).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }
    static post = function (url, params) {
        return new Promise((resolve, reject) => {
            axios.post(url, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }
}
export default API;

axios.interceptors.response.use(
    response => {
        // 如果返回的状态码为200，说明接口请求成功，可以正常拿到数据 
        // 否则的话抛出错误
        if (response.status === 200) {
            return Promise.resolve(response);
        } else {
            return Promise.reject(response);
        }
    },
    error => {
        if (error.response.status) {
            // switch (error.response.status) {
            // }
        }
    }
)
